<?php

namespace App\Http\Controllers\Talent;

use App\Http\Controllers\BaseController;
use App\Models\Job;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class JobController extends BaseController
{
    public function index(Request $request)
    {
        $params = $this->validate($request,[
            'keyword' => 'string|max:100',
            'post_id' => 'integer',
            'firm_profession_id' => 'integer',
            'province' => 'max:30',
            'city' => 'max:30',
            'area' => 'max:30',
        ]);

        $query = Job::query();

        if (isset($params['keyword'])){
            $query->where('post_title','like','%'.$params['keyword'].'%');
        }

        if (isset($params['post_id'])){
            $query->where('post_id',$params['post_id']);
        }

        if (isset($params['firm_profession_id'])){
            $query->where('firm_profession_id',$params['firm_profession_id']);
        }

        if (isset($params['province'])){
            $query->where('province',$params['province']);
        }

        if (isset($params['city'])){
            $query->where('city',$params['city']);
        }

        if (isset($params['area'])){
            $query->where('area',$params['area']);
        }


        $jobs = $query->orderByDesc('id')->simplePaginate();

        return $this->success($jobs);
    }

    public function show(Request $request)
    {
        $param = $this->validate($request,[
           'job_id' => 'required|integer'
        ]);

        $job = Job::query()->where('id',$param['job_id'])->first();
        DB::table('jobs')->where('id',$param['job_id'])->increment('view_count');

        return $this->success($job);
    }


    public function store(Request $request)
    {
        $param = $this->validate($request,[
           'post_id' => 'required',
           'post_title' => 'required|max:255',
           'firm_profession_id' => 'required',
           'province' => 'required|max:30',
           'city' => 'required|max:30',
           'area' => 'required|max:30',
           'contact_name' => 'required',
           'exp' => 'required',
           'curriculum_vitae' => 'required',
           'contact_phone' => 'required',
           'expected_salary' => 'required',
           'expect' => 'required',
        ]);

        $param['user_id'] = auth('api')->id();
        $job = Job::query()->create($param);

        if ($job->getKey()){
            return $this->success();
        }

        return $this->error();
    }
}
